package com.client.koshercook.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;

/* a widget used for selecting search filters */
public class Filters extends Composite{

	private PushButton open = new PushButton("סנן חיפוש");
	private Button ok = new Button("אישור");
	private Button cancel = new Button("ביטול");
	
	private VerticalPanel Vpanel = new VerticalPanel();
	private VerticalPanel VpanelUp = new VerticalPanel();
	private HorizontalPanel HpanelMid = new HorizontalPanel();
	private HorizontalPanel HpanelDown = new HorizontalPanel();
	
	private Label sitesHeader = new Label();
	private Label timeHeader = new Label();
	private DialogBox dBox = new DialogBox();
	private FlexTable dataTable = new FlexTable(); // the sites table
	private String[] sitesList = {"כושר-קוק", "כיפה", "השף הלבן","רוטב", "שטיבל"};
	private boolean[] changed = new boolean[sitesList.length]; //save if check box changed during last action
	
	private ListBox userMinutes = new ListBox();
		
	public Filters(){
		
		//init the list-box of user specified cook time
		userMinutes.addItem("ללא הגבלה","-1");	// -1 is used for non-limited
		userMinutes.addItem("עד 15 דקות","15");
		userMinutes.addItem("עד 30 דקות","30");
		userMinutes.addItem("עד 45 דקות","45");
		userMinutes.addItem("עד שעה","60");
		userMinutes.addItem("עד שעה וחצי","90");
		userMinutes.addItem("עד שעתיים","120");
		userMinutes.addItem("עד שלוש שעות","180");
		userMinutes.addItem("עד ארבע שעות","240");
		userMinutes.addItem("עד חמש שעות","300");
		userMinutes.addItem("עד שש שעות","360");
		userMinutes.setSelectedIndex(0);
		
		sitesHeader.setStyleName("midPanel");
		sitesHeader.setText("סמן את האתרים שכושר-קוק יבצע חיפוש בהם");
		sitesHeader.setHeight("25px");
		
		timeHeader.setStyleName("timeFilterText");	
		timeHeader.setText("הכנס מגבלת זמן הכנה");
		timeHeader.setTitle("זמן ההכנה של המתכונים שיופיעו לא יעלה על זמן זה");
		timeHeader.setHeight("25px");
		
		dataTable.setCellPadding(3);
		dataTable.setBorderWidth(2);
		dataTable.setStyleName("siteFilterText");
		
		open.setTitle("לחץ כדי להכניס סינונים נוספים לחיפוש");
		open.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				for(int i=0; i<changed.length; i++)
					changed[i] = false;
				dBox.center();
			}
		});
		
		cancel.setTitle("יציאה ללא שמירה");
		cancel.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				dBox.hide();
				for(int i=0; i<changed.length; i++){
					if (changed[i]){ //changed
						CheckBox cb = (CheckBox) dataTable.getWidget(i, 1) ;
						cb.setValue(!cb.getValue());
						dataTable.setWidget(i, 1, cb);
					}
				}
			}
		});
		
		ok.setTitle("יציאה עם שמירה");
		ok.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				//reseting filters
				Client.model.request.clearFilters();
				// get the sites to exclude from search
				boolean selected = false;
				for(int i=0; i < dataTable.getRowCount(); i++){
					CheckBox cb = (CheckBox) dataTable.getWidget(i, 1);
					if (!cb.getValue()){
						Client.model.request.addForbiddenSite(dataTable.getText(i, 0));
					}else{
						selected = true;
					}
				}
				if (!selected){ //no site has been chosen
					Client.model.setInfoTextAndPO("יש לבחור לפחות אתר אחד");
					return;
				}
				
				//get the cook time limit for the search
				String cookTimeText = userMinutes.getValue(userMinutes.getSelectedIndex());
				int cookTime = Integer.parseInt(cookTimeText);
				Client.model.request.setCookTimeLimit(cookTime);
				Client.model.setInfoTextAndPO("בקשתך נקלטה בהצלחה");
				dBox.hide();
				Client.model.setInfoText("d");
			}
		});

		//build the table
		buildTable();
		
		VpanelUp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		VpanelUp.add(sitesHeader);
		VpanelUp.add(dataTable);
		VpanelUp.setSpacing(15);
		
		HpanelMid.add(userMinutes);
		HpanelMid.add(timeHeader);
		HpanelMid.setSpacing(15);
		
		cancel.setSize("70", "25");
		cancel.setStyleName("largeButtons");
		ok.setSize("70", "25");
		ok.setStyleName("largeButtons");
		
		HpanelDown.add(cancel);
		HpanelDown.add(ok);
		HpanelDown.setSpacing(15);
		
		Vpanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		Vpanel.setSpacing(20);
		
		Vpanel.add(VpanelUp);
		Vpanel.add(HpanelMid);
		Vpanel.add(HpanelDown);
		Vpanel.setStyleName("filters");
		
		dBox.add(Vpanel);
		dBox.setText("הוסף סינון לחיפוש");
		dBox.setGlassEnabled(true);
		dBox.setAnimationEnabled(true);
		
		initWidget(open);
	}
	
	
	/** build site list */
	private void buildTable(){
		int rowN;
		for (String str : sitesList){
			rowN = addSite();
			dataTable.setText(rowN, 0, str);
			dataTable.getCellFormatter().addStyleName(rowN, 0,"FlexTable-ColumnLabelCell");
		}
	}

	
	/** add site */ 
	private int addSite(){
		final int rowN = dataTable.getRowCount();
		CheckBox mustButton = new CheckBox(); 	// Add a button to include this site in the search sites
		mustButton.setTitle("סמן כדי לחפש מתכונים מאתר זה");
		mustButton.setValue(true);
		mustButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				changed[rowN] = !changed[rowN];
			}
		});
		
		dataTable.setWidget(rowN, 1, mustButton);
		
		return rowN;
	}
	
 
	/** clear text fields and hide the dialog box */
	public void reset(){
		dataTable.removeAllRows();	
		buildTable();
		userMinutes.setSelectedIndex(0);
	}
}
